home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / libiberty / fdmatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-17  |  1.9 KB  |  70 lines

  1. /* Compare two open file descriptors to see if they refer to the same file.
  2.    Copyright (C) 1991 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18.  
  19. /*
  20.  
  21. NAME
  22.  
  23.     fdmatch -- see if two file descriptors refer to same file
  24.  
  25. SYNOPSIS
  26.  
  27.     int fdmatch (int fd1, int fd2)
  28.  
  29. DESCRIPTION
  30.  
  31.     Check to see if two open file descriptors refer to the same file.
  32.     This is useful, for example, when we have an open file descriptor
  33.     for an unnamed file, and the name of a file that we believe to 
  34.     correspond to that fd.  This can happen when we are exec'd with
  35.     an already open file (stdout for example) or from the SVR4 /proc
  36.     calls that return open file descriptors for mapped address spaces.
  37.     All we have to do is open the file by name and check the two file
  38.     descriptors for a match, which is done by comparing major&minor
  39.     device numbers and inode numbers.
  40.  
  41. BUGS
  42.  
  43.     (FIXME: does this work for networks?)
  44.     It works for NFS, which assigns a device number to each mount.
  45.  
  46. */
  47.  
  48. #include <sys/types.h>
  49. #include <sys/stat.h>
  50.  
  51. int fdmatch (fd1, fd2)
  52.     int fd1;
  53.     int fd2;
  54. {
  55.   struct stat sbuf1;
  56.   struct stat sbuf2;
  57.  
  58.   if ((fstat (fd1, &sbuf1) == 0) &&
  59.       (fstat (fd2, &sbuf2) == 0) &&
  60.       (sbuf1.st_dev == sbuf2.st_dev) &&
  61.       (sbuf1.st_ino == sbuf2.st_ino))
  62.     {
  63.       return (1);
  64.     }
  65.   else
  66.     {
  67.       return (0);
  68.     }
  69. }
  70.